<?php

/**
 * This is the model class for table "cashreceiptjournalitem".
 *
 * The followings are the available columns in table 'cashreceiptjournalitem':
 * @property string $id
 * @property string $cashReceiptJournalId
 * @property string $glId
 * @property string $glTransactionId
 * @property string $isDebit
 * @property string $amount
 * @property string $narration
 *
 * The followings are the available model relations:
 * @property Cashreceiptjournal $cashReceiptJournal
 * @property Gl $gl
 * @property Gltransaction $glTransaction
 */
class CashReceiptJournalItem extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cashreceiptjournalitem';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('cashReceiptJournalId, glId, glTransactionId, isDebit, amount', 'required'),
			array('cashReceiptJournalId, glId, glTransactionId, amount', 'length', 'max'=>10),
			array('isDebit', 'length', 'max'=>1),
			array('narration', 'length', 'max'=>255),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, cashReceiptJournalId, glId, glTransactionId, isDebit, amount, narration', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cashReceiptJournal' => array(self::BELONGS_TO, 'Cashreceiptjournal', 'cashReceiptJournalId'),
			'gl' => array(self::BELONGS_TO, 'Gl', 'glId'),
			'glTransaction' => array(self::BELONGS_TO, 'Gltransaction', 'glTransactionId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'cashReceiptJournalId' => 'Cash Receipt Journal',
			'glId' => 'Gl',
			'glTransactionId' => 'Gl Transaction',
			'isDebit' => 'Is Debit',
			'amount' => 'Amount',
			'narration' => 'Narration',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('cashReceiptJournalId',$this->cashReceiptJournalId,true);
		$criteria->compare('glId',$this->glId,true);
		$criteria->compare('glTransactionId',$this->glTransactionId,true);
		$criteria->compare('isDebit',$this->isDebit,true);
		$criteria->compare('amount',$this->amount,true);
		$criteria->compare('narration',$this->narration,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return CashReceiptJournalItem the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/* cast form class to model active record */
	public static function castFormToActiveRecord($formItems, $loadPersistRecord = FALSE)
	{
		$modelItems = array();
		foreach($formItems as $i => $item) 
		{
			if($loadPersistRecord && !empty($item->id))
				$modelItems[$i] = CashReceiptJournalItem::model()->findByPk($item->id);
			else
				$modelItems[$i] = new CashReceiptJournalItem();
			
			if($item->amountCredit == 0)
			{
				$modelItems[$i]->isDebit = 1;  
				$modelItems[$i]->amount = $item->amountDebit;  
			}
			else
			{
				$modelItems[$i]->isDebit = 0;  
				$modelItems[$i]->amount = $item->amountCredit;
			}
			$modelItems[$i]->glId = $item->glId;  
			$modelItems[$i]->narration = $item->narration;  
		}
		return $modelItems;
	}
	
	/* cast model active record to form class   */
	public static function castActiveRecordToForm($items)
	{
		$formItems = array();
		foreach($items as $i => $item) 
		{
			$formItems[$i] = new CashReceiptJournalItemForm();
			
			if($item->isDebit == 0)
			{
				$formItems[$i]->amountDebit = 0;  
				$formItems[$i]->amountCredit = $item->amount;  
			}
			else
			{
				$formItems[$i]->amountCredit = 0;  
				$formItems[$i]->amountDebit = $item->amount;  
			}
			$formItems[$i]->id = $item->id;  
			$formItems[$i]->glId = $item->glId;  
			$formItems[$i]->narration = $item->narration;  
		}
		return $formItems;
	}
}
